import { drawerConfig } from "./drawer";
import { modalConfig } from "./modal";
import { tableConfig } from "./table";
import { searchConfig } from "./search";
import { openModalInfo, setOpenModalInfo } from "../store";
import { fetchSearchMap } from "@userList/utils";
import { reviewQueryparams } from "../partyReview/store";
const listQueryParams = reactive<any>({});
export default defineComponent({
  props: {
    isPartyReview: {
      type: [Boolean],
      default: false,
    },
  },
  setup(props, { attrs, emit, expose, slots }) {
    const { proxy } = reactive<any>(getCurrentInstance());
    const queryParams = props.isPartyReview
      ? reviewQueryparams
      : listQueryParams;
    const showDrawer = () => (defineDrawer.visible.value = true);
    const showModal = () => (defineModal.visible.value = true);
    const handleAction = ({ record, type, callback }: any) => {
      const { title: drawerTitle, formState: drawerForm } = defineDrawer;
      const { title: modalTitle, formState: modalForm } = defineModal;
      switch (trimAll(type)) {
        case "detail":
          drawerTitle.value = props.isPartyReview
            ? $t("base.partyDetail")
            : $t("base.partyInfo");
          fetchDetail(record.id).then((res) => {
            showDrawer();
            Object.keys(drawerForm).forEach((key: any) => {
              drawerForm[key] = res[key] ?? drawerForm[key];
            });
            drawerForm.tags = res.tags ? res?.tags?.split?.(",") : [];
            drawerForm.tabType = 1;
            drawerForm.isShowGoReviewBtn = !props.isPartyReview;
            drawerForm.isShowReviewBtnGroup = props.isPartyReview;
            callback?.();
          });
          break;
        case "detail->member":
          drawerTitle.value = props.isPartyReview
            ? $t("base.partyDetail")
            : $t("base.partyInfo");
          fetchDetail(record.id).then((res) => {
            showDrawer();
            Object.keys(drawerForm).forEach((key: any) => {
              drawerForm[key] = res[key] ?? drawerForm[key];
            });
            drawerForm.tags = res.tags ? res?.tags?.split?.(",") : [];
            drawerForm.tabType = 2;
            drawerForm.isShowGoReviewBtn = true;
            drawerForm.isShowReviewBtnGroup = props.isPartyReview;
            callback?.();
          });
          break;
        case "review->reject":
          modalTitle.value = $t("base.refusePartyApply");
          Object.keys(modalForm).forEach((key: any) => {
            modalForm[key] = record[key];
          });
          modalForm.party_id = record.id;
          modalForm.type = 1;
          showModal();
          break;
        case "review->pass":
          proxy.$confirm({
            closable: true,
            maskClosable: true,
            icon: null,
            title: () => <>{$t("party49")}</>,
            style: {
              top: "30%",
            },
            content: (
              <>
                {$t("party50")}
                {record.name}
                {$t("party51")}
              </>
            ),
            onOk: async () => {
              modalTitle.value = $t("base.checkPass");
              Object.keys(modalForm).forEach((key: any) => {
                modalForm[key] = record[key] ?? modalForm[key];
              });
              modalForm.party_id = record.id;
              modalForm.type = 2;
              modalForm.tags = Array.isArray(modalForm.tags)
                ? modalForm.tags
                : [];
              showModal();
            },
          });
          break;
        case "cancel":
          defineDrawer.visible.value = false;
          fetchList();
          break;
        case "delete":
          proxy.$confirm({
            closable: true,
            maskClosable: true,
            icon: null,
            title: () => <>{$t("base.delete")}</>,
            style: {
              top: "30%",
            },
            content: (
              <>
                {$t("party52")}
                {record.name}
                {$t("party53")}
              </>
            ),
            onOk: async () => {
              try {
                const res: any = await societyApi.party.delete({
                  party_id: record.id,
                });
                if (res.code != 200) return message.error(res.message);
                message.success($t("base.deleteSuccess"));
                fetchList();
                callback?.();
              } catch (error: any) {
                message.error(error.message);
              } finally {
              }
            },
          });
          break;
      }
    };
    const refreshTable = async (searchParams: any = {}) => {
      defineTable.pagination.current = 1;
      initObjAttr(queryParams, searchParams);
      await fetchList();
    };
    const refrershDrawInfo = () => {
      if (openModalInfo.isOpenByDraw && defineDrawer.visible.value) {
        openModalInfo.drawCallback?.();
        setOpenModalInfo({
          isOpenByDraw: false,
          drawCallback: () => {},
        });
      } else {
        fetchList();
      }
    };
    const fetchDetail = async (party_id: string) => {
      const { loading } = defineTable;
      try {
        loading.value = true;
        const res: any = await societyApi.party.detail({
          party_id,
        });
        if (res.code != 200) return message.error(res.message);
        return res?.data;
      } catch (error: any) {
        message.error(error.message);
      } finally {
        loading.value = false;
      }
    };
    const fetchList = async () => {
      const { loading, pagination, dataSource } = defineTable;
      try {
        const party_time_start = queryParams?.["party_start_end"]?.[0]
          ? $dayjs(queryParams?.["party_start_end"]?.[0]).format("YYYY-MM-DD")
          : void 0;
        const party_time_end = queryParams?.["party_start_end"]?.[1]
          ? $dayjs(queryParams?.["party_start_end"]?.[1]).format("YYYY-MM-DD")
          : void 0;

        const operation_time_start = queryParams?.["operation_start_end"]?.[0]
          ? $dayjs(queryParams?.["operation_start_end"]?.[0]).format(
              "YYYY-MM-DD"
            )
          : void 0;
        const operation_time_end = queryParams?.["operation_start_end"]?.[1]
          ? $dayjs(queryParams?.["operation_start_end"]?.[1]).format(
              "YYYY-MM-DD"
            )
          : void 0;
        loading.value = true;
        dataSource.value = [];
        const res: any = await societyApi.party.query({
          page: pagination.current,
          page_size: pagination.pageSize,
          buss_id: queryParams.buss_id,
          party_name: queryParams.party_name,
          payment_method: queryParams.payment_method,
          initiator: queryParams.initiator,
          party_status: props.isPartyReview ? 0 : queryParams.party_status,
          party_time_start,
          party_time_end,
          operation_time_start,
          operation_time_end,
        });
        if (res.code != 200) return message.error(res.message);
        dataSource.value = res?.data?.list ?? [];
        pagination.total = res?.data?.attributes?.total ?? 0;
      } catch (error: any) {
        message.error(error.message);
      } finally {
        loading.value = false;
      }
    };

    const defineSearch = searchConfig({
      handleAction,
      refreshTable,
      isPartyReview: props.isPartyReview,
    });
    const defineDrawer = drawerConfig({
      handleAction,
      refreshTable: fetchList,
      fetchList,
    });
    const defineTable = tableConfig({
      handleAction,
      fetchList,
      isPartyReview: props.isPartyReview,
    });
    const initPage = () => {
      fetchList();
      fetchSearchMap();
    };
    expose({ initPage });
    const defineModal = modalConfig({ handleAction, refrershDrawInfo });
    return () => (
      <>
        <s-design
          defineTable={defineTable}
          defineSearch={defineSearch}
          defineDrawer={defineDrawer}
          defineModal={defineModal}
        />
      </>
    );
  },
});
